<?php

class DefaultController extends Controller
{
    public $layout = "//layouts/userpage";

    /**
     * @return array action filters
     */
    public function filters()
    {
        return CMap::mergeArray(parent::filters(),array(
            'accessControl', // perform access control for CRUD operations
        ));
    }
    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('view','inbox'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('index'),
                'users'=>array('@'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

    /**
     * Displays a particular model.
     */
    public function actionIndex($id) {
        $model = Users::model()->findByPk($id);
        $linkHire = Yii::app()->createUrl('job/create/hire',array('id'=>$id));
        setSession('linkhire',$linkHire);

        $model_skill = new UserSkill;
        $jobtype = Jobtype::model()->findAll();
        $skill = Skill::model()->findAll();
        $skills= null;
        $skills_add= null;
        $N= 0;
        $nj= 0;
        //save skill
        if(isset($_POST['skill']) || isset($_POST['Skill_add']) || isset($_POST['edit'])) {
            //echo("Bạn chưa chọn kỹ năng nào!");
            $skills = $_POST['skill'];
            $ni= count($skills);
            if(isset($_POST['Skill_add'])) {
                $skills_add = $_POST['Skill_add'];
                $nj= count($skills_add);
            }
            //echo $nj;
            $N= $ni + $nj;
            if($N > 10) {
                echo "Bạn đã chọn ".$N." kỹ năng, vượt quá 10 kỹ năng cho phép!";
            }
            else{
                $model_skill->user_id= $id;
                $like= array();
                $count = UserSkill::Model()->count("user_id=:user_id", array(":user_id" => $id));
                $delete= Yii::app()->db->createCommand("DELETE FROM tbl_user_skill WHERE user_id=$id AND skill_id!=0")->execute();
                for($i=0; $i< $ni; $i++) {
                    $si= $skills[$i];
                    $insert= Yii::app()->db->createCommand("INSERT INTO tbl_user_skill (user_id, skill_id) VALUES($id, $si)")->execute();
                }
                if(isset($_POST['edit'])) {
                    $arr = $_POST['edit'];
                    for($i=0; $i < count($_POST["id"]) ;$i++){
                        $skill2 = UserSkill::model()->findByPk($_POST['id'][$i]);
                        $skill2->skill_add=$_POST['edit'][$i];
                        $skill2->save(false);
                    }
                    // foreach ($_POST['edit'] as $key) {
                    //  $model_skill->user_id = 20;
                    //  $model_skill->skill_id = 0;
                    //  $model_skill->skill_add = "nhieen";

                    // }
                    // $model_skill->save();
                    
                }

                if(isset($_POST['Skill_add'])) {
                foreach ($skills_add as $key) {
                    if($key != null) {
                    //$model_skill->skill_add= $skills_add[$j];
                        $skill1 = new UserSkill;
                        $skill1->user_id= $id;
                        $skill1->skill_add= $key;
                        $skill1->skill_id= 0;
                        $skill1->save(false);
                    }
                }
                }
                $this->redirect(array('index', 'id'=>$id));
            }
        }
        //echo $_POST['edit'];

        if(isset($_POST['edit'])) {
            echo "123";
            echo $_POST['edit'];
            foreach ($_POST['edit'] as $key) {
                $skill2= new UserSkill;
                $del= Yii::app()->db->createCommand("DELETE FROM tbl_user_skill WHERE user_id=$id AND skill_id=0")->execute();
                //$skill2->skill_add= $key;
                $skill2->user_id= $id;
                $skill2->skill_add= $key;
                $skill2->skill_id= 0;
                //echo $key;
                $skill2->save(false);

                /*$criteria = new CDbCriteria;
                $criteria->condition='user_id='.$id;
                UserSkill::model()->updateAll($criteria);*/
            }
        }
        //save change log user
        $logs = UserLogs::model()->findAll("user_id = :id", array(':id'=>Yii::app()->user->id));
        $criteria = new CDbCriteria;
        $pages = new CPagination(count($logs));
        $pages->pageSize = count($logs);
        $pages->applyLimit($criteria);

        //render
        $this->render('index',array(
            'model'=>$model,
            'jobtype'=>$jobtype,
            'model_skill'=>$model_skill,
            'logs' => $logs,
            'pages' => $pages,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
     */
    public function loadUser($id=null)
    {
        if($this->_model===null)
        {
            if($id!==null || isset($_GET['id']))
                $this->_model=User::model()->findbyPk($id!==null ? $id : $_GET['id']);
            if($this->_model===null)
                throw new CHttpException(404,'The requested page does not exist.');
        }
        return $this->_model;
    }

    public function getCountJobtype($jobtype_id) {
        $count= 0;
        $skil= UserSkill::model()->findAll("user_id=:id",array(':id'=>Yii::app()->user->id));
        foreach ($skil as $key) {
            if($key->skill->subjobtype->jobtype->id == $jobtype_id){
                $count++;
            }
        }
        return $count;
    }
}
